package com.nd.qa.shareplate.script.selenium;

import com.nd.qa.shareplate.model.Module;
import com.nd.qa.shareplate.model.PageElement;
import com.nd.qa.shareplate.model.Project;
import com.nd.qa.shareplate.model.ProjectType;
import com.nd.qa.shareplate.util.CharacterUtils;

public class InitSeleniumScript {

	public String initSeleniumScript(Module module,ProjectType projectType,String scriptName,Project project){
		String platFormType = "Web";
		String initStr = "";
		String result = "";
		String loginUrl = "";
		//获取登录地址
		if (module.getModuleLoginUrl()!=null&&!"".equals(module.getModuleLoginUrl())) {
			loginUrl = module.getModuleLoginUrl();
		}else {
			loginUrl = project.getProjectLoginUrl();
		}
		initStr = "package com.nd.qa.selenium;\n"
				+"import com.nd.qa.selenium.common.SysConstant;\n"
				+"import com.nd.qa.selenium.handle.HandleData;\n"
				+"import com.nd.qa.selenium.model.TestCases;\n"
				+"import com.nd.qa.selenium.model.TestClass;\n"
				+"import com.nd.qa.selenium.tool.BaseWebDriver;\n"
				+"import com.nd.qa.selenium.tool.BaseWebDriver.BrowserType;\n"
				+"import com.nd.qa.selenium.util.DateUtil;\n"
				+"import junit.framework.TestCase;\n"
				+"public class %s extends TestCase{\n"
				+"		HandleData handle = new HandleData();\n"
				+"		BaseWebDriver baseWebDriver = null;\n"
				+"		private int classId = 0;\n"
				+"		private int projectId = 0;\n"
				+"		private String projectName = \"%s\";\n"
				+"		private String moduleName = \"%s\";\n"
				+"		protected void setUp() throws Exception {\n"
				+"			final String loginUrl = \"%s\";\n"
				+"			baseWebDriver = new BaseWebDriver(BrowserType.CHROME,loginUrl);\n"
				+"			TestClass testClass = new TestClass();\n"
				+"			projectId = %s;\n"
				+"			testClass.setClassName(Thread.currentThread().getStackTrace()[1].getClassName());\n"
				+"			testClass.setCreateTime(DateUtil.getSimpleDateFormat());\n"
				+"			testClass.setProjectId(projectId);\n"
				+"			if (0==SysConstant.TEST_CLASS_ID) {\n"
				+"				classId = handle.insert(testClass);\n"
				+"				SysConstant.TEST_CLASS_ID = classId;\n"
				+"			}else{\n"
				+"				classId = SysConstant.TEST_CLASS_ID;\n"
				+"			}\n"
				+"			super.setUp();\n"
				+"		}\n"
				+"		protected void tearDown() throws Exception {\n"
				+"			baseWebDriver.closeBrowser();\n"
				+"			super.tearDown();\n"
				+"			TestClass testClass = handle.queryClassInfo(classId);\n"
				+"			testClass.setClassId(classId);\n"
				+"			testClass.setTotalCase(SysConstant.TOTAL_CASE_COUNT);\n"
				+"			testClass.setEndTime(DateUtil.getSimpleDateFormat());\n"
				+"			handle.update(testClass);\n"
				+"		}\n";
				/*+"		public void test_Alogin() throws Exception {\n"
				+			"TestCases testCase = new TestCases();\n"
				+			"SysConstant.TOTAL_CASE_COUNT++;\n"
				+			"int stepId = 0;\n"
				+			"String method = Thread.currentThread() .getStackTrace()[1].getMethodName();\n"
				+			"testCase.setCaseName(method);\n"
				+			"testCase.setClassId(classId);\n"
				+			"testCase.setCreateTime(DateUtil.getSimpleDateFormat());\n"
				+			"int caseId = handle.insert(testCase);\n";
				if (null!=projectType.getBusinessId()&&!"".equals(projectType.getBusinessId())) {
					initStr = initStr+ "stepId = baseWebDriver.setTextFeildValue(\"TAG_NAME\",\"input\", 0, \""+projectType.getBusinessId()+"\",\"登录界面\", \"输入企业账号\", caseId);\n"
							+"stepId = baseWebDriver.setTextFeildValue(\"TAG_NAME\",\"input\", 1, \"uUserName\", \"登录界面\", \"输入账号\", caseId);\n"
							+"stepId = baseWebDriver.setTextFeildValue(\"TAG_NAME\",\"input\", 2, \"uPassword\", \"登录界面\", \"输入密码\", caseId);\n";
				}else{
					initStr = initStr+ "stepId = baseWebDriver.setTextFeildValue(\"TAG_NAME\",\"input\", 0, \"uUserName\", \"登录界面\", \"输入账号\", caseId);\n"
							+"stepId = baseWebDriver.setTextFeildValue(\"TAG_NAME\",\"input\", 1, \"uPassword\", \"登录界面\", \"输入密码\", caseId);\n";
				}
				
				initStr	= initStr+"baseWebDriver.clickWebElement(\"TAG_NAME\",\"button\",0, \"登录界面\", \"点击登录\", caseId);\n"
						+"testCase.setCaseId(caseId);\n"
						+"testCase.setStepCount(SysConstant.TOTAL_STEP_COUNT);\n"
						+"testCase.setCaseErrors(SysConstant.FAIL_STEP_COUNT);\n"
						+"testCase.setCaseFailures(SysConstant.FAIL_STEP_COUNT);\n"
						+"testCase.setEndTime(DateUtil.getSimpleDateFormat());\n"
						+"testCase.setSpendTime(DateUtil.getDiffTime(testCase.getEndTime(),testCase.getCreateTime()));\n"
						+"testCase.setSuccessRate((double)(SysConstant.TOTAL_STEP_COUNT-SysConstant.FAIL_STEP_COUNT)/(SysConstant.TOTAL_STEP_COUNT));\n"
						+"handle.update(testCase);\n"
						+"SysConstant.TOTAL_STEP_COUNT=0;\n"
						+"SysConstant.FAIL_STEP_COUNT=0;\n"
						+"}\n";*/
		 result = String.format(initStr,scriptName,module.getModuleName(),project.getProjectName(),loginUrl,project.getProjectId());
		return result;
	}
	
	/**
	 * 点击元素操作
	 * @param element
	 * @return
	 */
	public String click(PageElement element){
		String elementExpectContent = element.getElementExpectContent();
		String code = "";
		if ("2".equals(element.getIsIframeFlag())) {
			String iframeElementContent = "";
			if (element.getIframeElementContent().contains("\"")) {
				iframeElementContent = element.getIframeElementContent().replaceAll("\"", "'");
			}
			code = "baseWebDriver.switchToIFrame(\""+element.getIframeElementType()+"\",\""+iframeElementContent+"\");\n";
		}
		code = code+"baseWebDriver.clickWebElement(\"%s\",\"%s\", %s, %s, \"%s\", \"%s\", caseId);\n";
		//增加校验的功能
		if (element.getElementExpectResult()!=null&&elementExpectContent!=null&&!"".equals(elementExpectContent)) {
			if (elementExpectContent.contains("\"")) {
				elementExpectContent = elementExpectContent.replaceAll("\"", "'");
			}
			code = code+ "baseWebDriver.checkWebElement(\"%s\", \"%s\", %s, %s, \"%s\", \"%s\", stepId);\n";
		}else{
			if ("2".equals(element.getOverIframeFlag())) {
				code = code+"baseWebDriver.switchToDefaultContent();\n";
			}
			if ("A".equals(element.getIsSwitchPage())) {
				code = code+"String window"+element.getElementId()+" = baseWebDriver.currentWindow();\n baseWebDriver.switchToNewWindow(window"+element.getElementId()+");\n";
			}
			return String.format(code,element.getElementTypeName(), element.getElementContent(),
					element.getElementOrder(),element.getElementWaitTime(),element.getPageName(),element.getElementOperateDesc());
		}
		if ("2".equals(element.getOverIframeFlag())) {
			code = code+"baseWebDriver.switchToDefaultContent();\n";
		}
		if ("A".equals(element.getIsSwitchPage())) {
			code = code+"String window"+element.getElementId()+" = baseWebDriver.currentWindow();\n baseWebDriver.switchToNewWindow(window"+element.getElementId()+");\n";
		}
		return String.format(code,element.getElementTypeName(), element.getElementContent(),
				element.getElementOrder(),element.getElementWaitTime(),element.getPageName(),element.getElementOperateDesc(),element.getElementExpectName()
				,elementExpectContent,element.getElementExpectOrder(),element.getElementWaitTime(),element.getElementExpectType(),element.getElementExpectResult());
		
	}
	
	/**
	 * 点击元素操作
	 * @param element
	 * @return
	 */
	public String focusClick(PageElement element){
		String elementExpectContent = element.getElementExpectContent();
		String code = "";
		code = code+"baseWebDriver.focusClick(\"%s\",\"%s\", \"%s\", \"%s\",%s, \"%s\",\"%s\", caseId);\n";
		//增加校验的功能
		if (element.getElementExpectResult()!=null&&elementExpectContent!=null&&!"".equals(elementExpectContent)) {
			if (elementExpectContent.contains("\"")) {
				elementExpectContent = elementExpectContent.replaceAll("\"", "'");
			}
			code = code+ "baseWebDriver.checkWebElement(\"%s\", \"%s\", %s,%s, \"%s\", \"%s\", stepId);\n";
		}else{
			if ("2".equals(element.getOverIframeFlag())) {
				code = code+"baseWebDriver.switchToDefaultContent();\n";
			}
			if ("A".equals(element.getIsSwitchPage())) {
				code = code+"String window"+element.getElementId()+" = baseWebDriver.currentWindow();\n baseWebDriver.switchToNewWindow(window"+element.getElementId()+");\n";
			}
			return String.format(code,element.getElementTypeName(), element.getElementContent(),
					element.getFocusTypeName(),element.getElementText(),element.getElementWaitTime(),element.getPageName(),element.getElementOperateDesc());
		}
		return String.format(code,element.getElementTypeName(), element.getElementContent(),element.getFocusTypeName(),element.getElementText(),
				element.getPageName(),element.getElementOperateDesc(),element.getElementExpectName()
				,elementExpectContent,element.getElementExpectOrder(),element.getElementWaitTime(),element.getElementExpectType(),element.getElementExpectResult());
		
	}
	/**
	 * 键盘操作
	 * @param element
	 * @return
	 */
	public String keys(PageElement element){
		String code = "baseWebDriver.physicsAction(\"%s\");\n";
		return String.format(code,element.getElementTypeName());
	}
	/**
	 * 坐标点击操作
	 * @param element
	 * @return
	 */
	public String clickPoint(PageElement element){
		//解析element.getElementContent()
		String[] xyStr = element.getElementContent().split(",");
		String code = "stepId =driver.elementsByLinkText(driver,\"%s\",%s,%s,\"%s\",\"%s\",\"%s\",caseId);\n";
		return String.format(code,"click",Integer.valueOf(xyStr[0]),Integer.valueOf(xyStr[1]),element.getPageName(),element.getElementOperateDesc());
	}
	/**
	 * 输入框操作
	 * @param element
	 * @return
	 */
	public String sendKey(PageElement element){
		String code = "";
		String elementText = element.getElementText();
		if ("Y".equals(element.getElementTextFlag())) {
			elementText = elementText+CharacterUtils.getRandomString2(4);
		}
		if ("2".equals(element.getIsIframeFlag())) {
			code = "baseWebDriver.switchToIFrame(\""+element.getIframeElementType()+"\",\""+element.getIframeElementContent()+"\");\n";
		}
		code =code+ "stepId = baseWebDriver.setTextFeildValue(\"%s\",\"%s\", %s, \"%s\",%s,\"%s\", \"%s\", caseId);\n";
		//增加校验的功能
		if (element.getElementExpectResult()!=null&&element.getElementExpectContent()!=null) {
			code = code+ "baseWebDriver.checkWebElement(\"%s\", \"%s\", %s,%s, \"%s\", \"%s\", stepId);\n";
		}else{
			if ("2".equals(element.getOverIframeFlag())) {
				code = code+"baseWebDriver.switchToDefaultContent();\n";
			}
			return String.format(code,element.getElementTypeName(),element.getElementContent(),element.getElementOrder(),elementText,element.getElementWaitTime(),element.getPageName(),element.getElementOperateDesc());
		}
		if ("2".equals(element.getOverIframeFlag())) {
			code = code+"baseWebDriver.switchToDefaultContent();\n";
		}
		return String.format(code,element.getElementTypeName(),element.getElementContent(),element.getElementOrder(),elementText,element.getElementWaitTime(),element.getPageName(),element.getElementOperateDesc(),
				element.getElementExpectName(),element.getElementExpectContent(),element.getElementExpectOrder(),element.getElementWaitTime(),element.getElementExpectType(),
				element.getElementExpectResult());
	}
	/**
	 * 下拉框选择
	 * @param element
	 * @return
	 */
	public String selectDropdownList(PageElement element){
		String code = "";
		if ("2".equals(element.getIsIframeFlag())) {
			code = "baseWebDriver.switchToIFrame(\""+element.getIframeElementType()+"\",\""+element.getIframeElementContent()+"\");\n";
		}
		code =code+ "stepId = baseWebDriver.selectDropdownList(\"%s\",\"%s\", \"%s\", \"%s\",\"%s\",caseId);\n";
		if ("2".equals(element.getOverIframeFlag())) {
			code = code+"baseWebDriver.switchToDefaultContent();\n";
		}
		return String.format(code,element.getElementTypeName(),element.getElementContent(),element.getElementText(),element.getPageName(),element.getElementOperateDesc());
	}
}
